package icu.zqyhdm.blog.controller;

import icu.zqyhdm.blog.vo.CommentListVo;
import icu.zqyhdm.blog.vo.CommentUploadVo;
import icu.zqyhdm.blog.vo.CommentVo;
import icu.zqyhdm.blog.vo.ReplyVo;
import icu.zqyhdm.result.Result;
import icu.zqyhdm.blog.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description
 * @Author lifu
 * @Date 2021/6/23 21:39
 */
@Api(tags = "文章的评论接口")
@RestController
@RequestMapping(value = "/api/blog/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 查出所有评论以及子评论
     */
    @ApiOperation(value = "文章评论查询")
    @GetMapping(value = "/listAll")
    public Result listAll(){
        List<CommentVo> commentVoList = commentService.listAll();
        return Result.ok(commentVoList);
    }

    /**
     * 查出某个文章的所有评论以及子评论，以树形结构组装起来
     */
    @ApiOperation(value = "查出某个文章的所有评论以及子评论")
    @GetMapping(value = "/list/{articleId}/{current}/{limit}")
    public Result list(@PathVariable Integer articleId,
                       @PathVariable Integer current,
                       @PathVariable Integer limit){
        CommentListVo commentListVo = commentService.list(articleId,current,limit);
        return Result.ok(commentListVo);
    }

    /**
     * 查询评论下的回复
     */
    @ApiOperation(value = "查询评论下的回复")
    @GetMapping("/replies/{articleId}/{commentId}/{current}/{limit}")
    public Result listReplyByCommentId(@PathVariable Integer articleId,
                                       @PathVariable Integer commentId,
                                       @PathVariable Integer current,
                                       @PathVariable Integer limit) {
        List<ReplyVo> replyVoList = commentService.listRepliesByCommentId(articleId,commentId,current,limit);
        return Result.ok(replyVoList);
    }

    @ApiOperation(value = "添加文章评论")
    @PostMapping(value = "/add")
    public Result addType(@RequestHeader("X-Token") String token, @RequestBody CommentUploadVo commentUploadVo) {
        commentService.saveComment(token, commentUploadVo);
        return Result.ok();
    }

    /**
     * 评论点赞
     *
     * @param commentId 评论id
     * @return {@link Result<>}
     */
    @ApiOperation(value = "评论点赞")
    @PostMapping("/like/{articleId}/{commentId}")
    public Result likeComment(@RequestHeader("X-Token") String token,
                              @PathVariable("articleId") Integer articleId,
                              @PathVariable("commentId") Integer commentId) {
        commentService.likeComment(token,articleId,commentId);
        return Result.ok();
    }
}
